package com.example.forum.config;

import com.example.forum.interceptor.LoginInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.io.File;

/**
 * Web MVC 配置类
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Bean
    public LoginInterceptor loginInterceptor() {
        return new LoginInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 登录拦截器
        registry.addInterceptor(loginInterceptor())
                // 需要登录才能访问的路径 - 只拦截个人相关功能
                // 用户个人信息相关
                .addPathPatterns("/user/info")
                .addPathPatterns("/user/modifyInfo")
                .addPathPatterns("/user/modifyPwd")
                .addPathPatterns("/user/favorites")
                // 文章操作相关（只拦截需要操作的，查看不拦截）
                .addPathPatterns("/article/create")
                .addPathPatterns("/article/modify")
                .addPathPatterns("/article/delete")
                .addPathPatterns("/article/thumbsUp")
                .addPathPatterns("/article/cancelThumbsUp")
                // 评论相关（只拦截需要操作的，查看不拦截）
                .addPathPatterns("/reply/create")
                .addPathPatterns("/reply/delete")
                // 个人消息相关
                .addPathPatterns("/message/**")
                // 管理员操作相关
                .addPathPatterns("/admin/**")
                
                // 排除不需要登录就能访问的路径
                .excludePathPatterns("/user/login")
                .excludePathPatterns("/user/register")
                .excludePathPatterns("/user/active")
                // 文章浏览、查询相关（公共功能）
                .excludePathPatterns("/article/getDetails")
                .excludePathPatterns("/article/getSortedArticles")
                .excludePathPatterns("/article/getAllByUserId")
                // 板块相关（公共功能）
                .excludePathPatterns("/board/**")
                // 公告相关（公共功能）
                .excludePathPatterns("/announcement/**")
                // 评论查看（公共功能）
                .excludePathPatterns("/reply/getReplies")
                // 排除静态资源路径，确保图片等静态资源可以直接访问
                .excludePathPatterns("/upload/**")
                .excludePathPatterns("/upload/images/**")
                .excludePathPatterns("/upload/images/**/*.jpg")
                .excludePathPatterns("/upload/images/**/*.png")
                .excludePathPatterns("/upload/images/**/*.jpeg")
                .excludePathPatterns("/upload/images/**/*.gif")
                .excludePathPatterns("/upload/avatars/**")
                .excludePathPatterns("/static/**")
                .excludePathPatterns("/**/*.css")
                .excludePathPatterns("/**/*.js")
                .excludePathPatterns("/**/*.png")
                .excludePathPatterns("/**/*.jpg")
                .excludePathPatterns("/**/*.jpeg")
                .excludePathPatterns("/**/*.gif");
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 映射upload目录为静态资源，使上传的图片可通过URL访问
        String uploadPath = System.getProperty("user.dir") + File.separator + "upload";
        
        // 添加资源处理器，将/upload/**映射到实际的upload目录
        registry.addResourceHandler("/upload/**")
                .addResourceLocations("file:" + uploadPath + File.separator)
                .setCachePeriod(3600)  // 缓存1小时
                .resourceChain(true);  // 启用资源链，提高性能
        
        // 特别针对/upload/images/路径的直接映射
        registry.addResourceHandler("/upload/images/**")
                .addResourceLocations("file:" + uploadPath + "/images/")
                .setCachePeriod(3600)
                .resourceChain(true);
        
        // 特别针对/upload/avatars/路径的直接映射
        registry.addResourceHandler("/upload/avatars/**")
                .addResourceLocations("file:" + uploadPath + "/avatars/")
                .setCachePeriod(3600)
                .resourceChain(true);
        
        // 打印日志便于调试
        System.out.println("静态资源映射配置1: /upload/** -> file:" + uploadPath + File.separator);
        System.out.println("静态资源映射配置2: /upload/images/** -> file:" + uploadPath + "/images/");
        System.out.println("静态资源映射配置3: /upload/avatars/** -> file:" + uploadPath + "/avatars/");
        
        // 同时设置Classpath下的静态资源映射
        registry.addResourceHandler("/static/**")
                .addResourceLocations("classpath:/static/")
                .setCachePeriod(3600)
                .resourceChain(true);
        
        System.out.println("静态资源映射配置4: /static/** -> classpath:/static/");
    }
} 